module.exports = function(io){
    var socketList = {};
    // 在线人数
    let member = 0
    io.on('connection',(socket)=>{
        // console.log('socket 连接成功');
        // 加入成员
        socket.on('join',data=>{
            // console.log(socket);
            // console.log('有人加入进来了',data);
            socket.name = data.username;
            socket.profile = data.profile;
            socketList.name = socket.name
            member++

            // 加入成员过后广播欢迎信息
            socket.broadcast.emit('welcome',data,member)
            socket.emit('welcome',data,member)
        })
        // 接收信息
        socket.on('msg',data=>{
            // console.log('接收到了',data);
            // 广播消息
            socket.broadcast.emit('broadcastMsg',data)
        })
        // 用户离开
        socket.on('disconnecting',function(){
            let userInfo = {'name':socket.name, 'profile':socket.profile}
            // console.log(userInfo,'离开了');
            if(socketList.hasOwnProperty(socket.name)){
                // 删除
                delete socketList.name;
                member--
                // 广播到所有用户
                
                socket.broadcast.emit('quit',userInfo,member) 
            }
            socket.broadcast.emit('quit',userInfo,member) 
        })
    })
}